package com.pandora.credit.mongo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.pandora.common.util.Constants;
import com.pandora.credit.dto.response.LoanResponseNextNo;
import com.pandora.credit.dto.response.PicMongoBannerListResponse;
import com.pandora.credit.dto.response.PicMongoBannerNoIdResponse;
import com.pandora.credit.dto.response.PicMongoBannerResponse;
import com.pandora.credit.mongo.dao.MongoBannerDao;
import com.pandora.credit.mongo.entity.MongoActivity;
import com.pandora.credit.mongo.entity.MongoBanner;
import com.pandora.credit.mongo.service.MongoBannerService;
import com.pandora.credit.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author developer
 */
@Service
public class MongoBannerServiceImpl implements MongoBannerService {

    @Autowired
    private MongoBannerDao mongoBannerDao;
    @Autowired
    private CommonService commonService;

    @Override
    public List<MongoBanner> queryByTypeStatus(int type, int status) {
        return mongoBannerDao.selectByTypeStatus(type, status);
    }

    @Override
    public LoanResponseNextNo doGetBanners(String ua) {
        List<Map<String, Object>> itemsIos = new LinkedList<Map<String, Object>>();
        List<Map<String, Object>> items = new LinkedList<Map<String, Object>>();
        int value = commonService.getAppStatusLimit(ua);
        List<MongoBanner> bannerList = queryByTypeStatus(Constants.BANNER_TYPE_HOME_BANNER, 1);
        for(MongoBanner mb : bannerList){
            String name = mb.getName();
            String destUrl = mb.getDestUrl();
            String imgUrl = mb.getImgUrl();
            MongoActivity.MongoActivityShare sharing = mb.getSharing();
            Map<String, Object> dict = new LinkedHashMap<String, Object>();
            dict.put("dest_url", destUrl);
            dict.put("name", name);
            dict.put("img_url", imgUrl);
            dict.put("sharing", sharing);
            if("外卖活动 滴滴出行活动".contains(name)){
                itemsIos.add(dict);
            }else {
                items.add(dict);
            }
        }
        if(value == Constants.VERSION_STATUS_CHECKING){
            return new LoanResponseNextNo(Constants.RETURN_TYPE_BANNER, itemsIos);

        }else {
            return new LoanResponseNextNo(Constants.RETURN_TYPE_BANNER, items);
        }
    }

    @Override
    public Object doGetBannerWithType(int type) {
        Object re = null;
        List<MongoBanner> banners = queryByTypeStatus(type, 1);
        if(banners != null || banners.size() > 0){
            if(type == Constants.BANNER_TYPE_LOAN_BANNER){
                List<PicMongoBannerResponse> list = new LinkedList<>();
                for(MongoBanner mb : banners){
                    PicMongoBannerResponse pbr = new PicMongoBannerResponse(mb.getId(), mb.getName(),
                            mb.getDestUrl(), mb.getImgUrl(), mb.getSharing());
                    list.add(pbr);
                }
                PicMongoBannerListResponse items = new PicMongoBannerListResponse(list);
                re = items;
            }else if(type == Constants.BANNER_TYPE_CALCULATOR_BANNER){
                MongoBanner mongoBanner = banners.get(0);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", mongoBanner.getName());
                jsonObject.put("dest_url", mongoBanner.getDestUrl());
                jsonObject.put("img_url", mongoBanner.getImgUrl());
                jsonObject.put("sharing", mongoBanner.getSharing());
                re = jsonObject.toString();

                PicMongoBannerNoIdResponse picMongoBannerNoIdResponse = new PicMongoBannerNoIdResponse(
                        mongoBanner.getName(), mongoBanner.getDestUrl(),
                        mongoBanner.getImgUrl(), mongoBanner.getSharing());
                re = picMongoBannerNoIdResponse;
            }
        }
        return re;
    }
}
